package com.tiancheng.trade.authserver.utils;

import com.tiancheng.trade.authserver.constant.SystemConstant;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.commom.core.utils.ServiceLocator;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 订单缓存
 *
 * @author kellen
 * @date 2024/3/17
 */
public class AuthCacheUtil {

    private static final RedissonClient redissonClient = ServiceLocator.getBean(RedissonClient.class);

    // oauth2授权码信息key
    private static final String OAUTH2_AUTH_CODE = "auth:oauth2:auth:code:";
    /**
     * oauth2 颁发的access token缓存前缀
     */
    public static final String OAUTH2_ACCESS_TOKEN_CACHE_PREFIX = "auth:oauth2:accessToken:";



    /**
     * 缓存oauth2授权code
     *
     * @param authCode
     * @param authCodeInfo
     */
    public static void cacheOauth2AuthCode(String authCode, HashMap<String, String> authCodeInfo) {
        String key = new StringBuilder(OAUTH2_AUTH_CODE).append("_").append(authCode).toString();
        RBucket<String> bucket = redissonClient.getBucket(key);
        bucket.set(JsonUtil.toJsonString(authCodeInfo), Duration.of(SystemConstant.oauth2AuthCodeTimeout, ChronoUnit.MINUTES));
    }

    /**
     * 获取oauth2授权code
     *
     * @param authCode
     * @return
     */
    public static Optional<Map<String, String>> getOauth2AuthCode(String authCode) {
        String key = new StringBuilder(OAUTH2_AUTH_CODE).append("_").append(authCode).toString();
        RBucket<String> bucket = redissonClient.getBucket(key);
        String payInfo = bucket.get();
        if (StringUtils.isNotEmpty(payInfo)) {
            Map<String, String> payInfoMap = JsonUtil.fromJson(payInfo, Map.class);
            return Optional.of(payInfoMap);
        }
        return Optional.empty();
    }

    /**
     * 删除oauth2授权code
     *getPrePayInfo
     * @param authCode
     */
    public static void delOauth2AuthCode(String authCode) {
        String key = new StringBuilder(OAUTH2_AUTH_CODE).append("_").append(authCode).toString();
        RBucket<String> bucket = redissonClient.getBucket(key);
        bucket.delete();
    }

    // ------------------------------------------------------------AccessToken cache


    /**
     * 缓存oauth2 accessToken
     *
     * @param authCode
     * @param authCodeInfo
     */
    public static void cacheOauth2AccessToken(String authCode, HashMap<String, String> authCodeInfo) {
        String key = new StringBuilder(OAUTH2_ACCESS_TOKEN_CACHE_PREFIX).append("_").append(authCode).toString();
        RBucket<String> bucket = redissonClient.getBucket(key);
        bucket.set(JsonUtil.toJsonString(authCodeInfo), Duration.of(SystemConstant.oauth2AuthCodeTimeout, ChronoUnit.MINUTES));
    }

    /**
     * 获取oauth2 accessToken
     *
     * @param accessToken
     * @return
     */
    public static Optional<Map<String, String>> getOauth2AccessToken(String accessToken) {
        String key = new StringBuilder(OAUTH2_ACCESS_TOKEN_CACHE_PREFIX).append("_").append(accessToken).toString();
        RBucket<String> bucket = redissonClient.getBucket(key);
        String payInfo = bucket.get();
        if (StringUtils.isNotEmpty(payInfo)) {
            Map<String, String> payInfoMap = JsonUtil.fromJson(payInfo, Map.class);
            return Optional.of(payInfoMap);
        }
        return Optional.empty();
    }

    /**
     * 删除oauth2授权code
     *getPrePayInfo
     * @param accessToken
     */
    public static void delOauth2AccessToken(String accessToken) {
        String key = new StringBuilder(OAUTH2_AUTH_CODE).append("_").append(accessToken).toString();
        RBucket<String> bucket = redissonClient.getBucket(key);
        bucket.delete();
    }

}
